/*--------管理员ui界面-----------*/
#include"admin_ui.h"
#include"stdlib.h"
#include"admin_system.h"
#include"value.h"
#include"login_ui.h"
#include"goods_list_op.h"
#include"admin_system_user_manage.h"

/**
 * 管理员ui初始化
*/
void admin_ui_init(void)
{
    /*========1.创建管理员主界面容器==========*/
    lv_obj_t *admin_cont = lv_obj_create(lv_scr_act());
    lv_obj_set_size(admin_cont,800,480); // 800 * 480
    //lv_obj_set_scrollbar_mode(admin_cont,LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_border_width(admin_cont, 0, 0);
    lv_obj_set_style_pad_all(admin_cont, 0, 0);
    //    背景
    lv_obj_t *img = lv_img_create(admin_cont);
    //    设置图片源
    LV_IMG_DECLARE(QQ);
    lv_img_set_src(img,&QQ);
    lv_obj_set_size(img,800,480);
    // lv_obj_set_pos(img,0,0);

    //   1.1 管理员界面标签
    lv_obj_t *manage_label = lv_label_create(admin_cont);
    lv_obj_set_size(manage_label,300,50);
    lv_label_set_text(manage_label,"Manage System");
    lv_obj_set_style_text_font(manage_label,&lv_font_montserrat_30,0);
    lv_obj_set_pos(manage_label,250,0);

    /*========2.创建两个子容器:商品管理+用户管理=======*/
    //   2.1 ---创建商品管理容器 ---
    lv_obj_t *goods_manage = lv_obj_create(admin_cont);
    lv_obj_set_size(goods_manage,260,350); //240 * 300
    // lv_obj_set_align(goods_manage,LV_ALIGN_LEFT_MID);
    lv_obj_align(goods_manage,LV_ALIGN_LEFT_MID,10,0);
    lv_obj_set_scrollbar_mode(goods_manage,LV_SCROLLBAR_MODE_OFF);

    //   2.1.1 创建商品管理标签
    lv_obj_t *goods_manage_label = lv_label_create(goods_manage);
    lv_label_set_text(goods_manage_label,"Goods Manage");
    lv_obj_set_style_text_font(goods_manage_label,&lv_font_montserrat_20,0);
    lv_obj_set_pos(goods_manage_label,40,0);

    //  2.1.2 创建商品管理按钮:上架、下架、库存管理、售价编辑 
    //                  上架按钮(已完成)
    lv_obj_t *put_goods_btn = lv_btn_create(goods_manage);
    lv_obj_set_size(put_goods_btn,130,40);
    lv_obj_set_pos(put_goods_btn,45,50);  
    lv_obj_add_event_cb(put_goods_btn,create_input_window, LV_EVENT_CLICKED, NULL);
    
    lv_obj_t *put_goods_label = lv_label_create(put_goods_btn);
    lv_label_set_text(put_goods_label,"Put goods");
    lv_obj_set_style_text_font(put_goods_label,&lv_font_montserrat_16,0);
    lv_obj_center(put_goods_label);
    //                  下架按钮
    lv_obj_t *remove_goods_btn = lv_btn_create(goods_manage);
    lv_obj_set_size(remove_goods_btn,130,40);
    lv_obj_set_pos(remove_goods_btn,45,100);  
    lv_obj_add_event_cb(remove_goods_btn,create_input_window,LV_EVENT_CLICKED,NULL);

    lv_obj_t *remove_goods_label = lv_label_create(remove_goods_btn);
    lv_label_set_text(remove_goods_label,"Remove goods");
    lv_obj_set_style_text_font(remove_goods_label,&lv_font_montserrat_16,0);
    lv_obj_center(remove_goods_label);
    //                  库存管理按钮
    lv_obj_t *inventory_btn = lv_btn_create(goods_manage);
    lv_obj_set_size(inventory_btn,130,40);
    lv_obj_set_pos(inventory_btn,45,150);  
    lv_obj_add_event_cb(inventory_btn,create_input_window,LV_EVENT_CLICKED,NULL);
    lv_obj_t *inventory_label = lv_label_create(inventory_btn);
    lv_label_set_text(inventory_label,"inventory");
    lv_obj_set_style_text_font(inventory_label,&lv_font_montserrat_16,0);
    lv_obj_center(inventory_label);
    //                  售价编辑按钮
    lv_obj_t *price_editor_btn = lv_btn_create(goods_manage);
    lv_obj_set_size(price_editor_btn,130,40);
    lv_obj_set_pos(price_editor_btn,45,200);  
    lv_obj_add_event_cb(price_editor_btn,create_input_window,LV_EVENT_CLICKED,NULL);

    lv_obj_t *price_editor_label = lv_label_create(price_editor_btn);
    lv_label_set_text(price_editor_label,"price editor");
    lv_obj_set_style_text_font(price_editor_label,&lv_font_montserrat_16,0);
    lv_obj_center(price_editor_label);

    //                   显示商品信息按钮
    lv_obj_t *goods_info_btn = lv_btn_create(goods_manage);
    lv_obj_set_size(goods_info_btn,130,40);
    lv_obj_set_pos(goods_info_btn,45,250);  
    lv_obj_add_event_cb(goods_info_btn,create_input_window,LV_EVENT_CLICKED,NULL);
    lv_obj_t *goods_info_label = lv_label_create(goods_info_btn);
    lv_label_set_text(goods_info_label,"show goods");
    lv_obj_set_style_text_font(goods_info_label,&lv_font_montserrat_16,0);
    lv_obj_center(goods_info_label);

    //  2.2  ---管理vip用户的容器---
    lv_obj_t *user_manage = lv_obj_create(admin_cont);
    lv_obj_set_size(user_manage,260,350); //240 * 300
    lv_obj_align_to(user_manage,goods_manage,LV_ALIGN_OUT_RIGHT_MID,10,0);
    lv_obj_set_scrollbar_mode(user_manage,LV_SCROLLBAR_MODE_OFF);

    //   2.2.1 创建商品管理标签
    lv_obj_t *user_manage_label = lv_label_create(user_manage);
    lv_label_set_text(user_manage_label,"User Manage");
    lv_obj_set_style_text_font(user_manage_label,&lv_font_montserrat_20,0);
    lv_obj_set_pos(user_manage_label,40,0);

    //   2.2.2  创建商品管理按钮：修改用户信息 、折扣设置
    //      修改用户信息
    lv_obj_t *modify_user_btn = lv_btn_create(user_manage);
    lv_obj_set_size(modify_user_btn,160,40);
    lv_obj_set_pos(modify_user_btn,35,50);  
    lv_obj_add_event_cb(modify_user_btn,create_input_window,LV_EVENT_CLICKED,NULL);

    lv_obj_t *modify_user_label = lv_label_create(modify_user_btn);
    lv_label_set_text(modify_user_label,"Modify user");
    lv_obj_set_style_text_font(modify_user_label,&lv_font_montserrat_16,0);
    lv_obj_center(modify_user_label);
    //   折扣设置
    // lv_obj_t *Discount_btn = lv_btn_create(user_manage);
    // lv_obj_set_size(Discount_btn,160,40);
    // lv_obj_set_pos(Discount_btn,35,100);  
    // lv_obj_add_event_cb(Discount_btn,create_input_window,LV_EVENT_CLICKED,NULL);

    // lv_obj_t *Discount_label = lv_label_create(Discount_btn);
    // lv_label_set_text(Discount_label,"Discount setting");
    // lv_obj_set_style_text_font(Discount_label,&lv_font_montserrat_16,0);
    // lv_obj_center(Discount_label);

    // 3. 管理员相关信息
    lv_obj_t *log_img = lv_img_create(admin_cont);
    //设置图片源
    LV_IMG_DECLARE(qq_log);
    lv_img_set_src(log_img,&qq_log);
    // lv_obj_set_pos(log_img,20,20);
    lv_obj_align(log_img,LV_ALIGN_TOP_RIGHT,-20,20);
    lv_obj_set_size(log_img,190,190);
    lv_obj_set_style_radius(log_img,100,0);
    lv_obj_set_style_clip_corner(log_img,60,0);

    // 4.退出按钮
    lv_obj_t *manage_quit_btn = lv_btn_create(admin_cont);
    lv_obj_set_size(manage_quit_btn,80,50);
    lv_obj_align(manage_quit_btn,LV_ALIGN_BOTTOM_RIGHT,0,0);
    lv_obj_set_style_bg_color(manage_quit_btn,lv_palette_main(LV_PALETTE_PINK),0);
    
    lv_obj_t *manage_quit_label = lv_label_create(manage_quit_btn);
    lv_label_set_text(manage_quit_label,"Quit");
    lv_obj_set_style_text_font(manage_quit_label,&lv_font_montserrat_20,0);
    lv_obj_center(manage_quit_label);
    lv_obj_add_event_cb(manage_quit_btn,return_origin_event_cb,LV_EVENT_CLICKED,NULL);
    
}

// 添加商品中完成按钮事件回调
void finish_btn_event(lv_event_t * e) {
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        // 获取输入内容
        const char * id_str = lv_textarea_get_text(goods_id_ta);
        const char * name_str = lv_textarea_get_text(goods_name_ta);
        const char * quantity_str = lv_textarea_get_text(goods_quantity_ta);
        const char * price_str = lv_textarea_get_text(goods_price_ta);
        char type_str[32];
        lv_dropdown_get_selected_str(goods_type_ta,type_str,32);
        // 输入验证
        if(strlen(id_str) == 0 || strlen(name_str) == 0 || 
           strlen(quantity_str) == 0 || strlen(price_str) == 0 || strlen(type_str) == 0) {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "error", "input not null", NULL, true);
            lv_obj_center(mbox);
            return;
        }

        // 转换数据类型
        int id = atoi(id_str);
        int quantity = atoi(quantity_str);
        float price = atof(price_str);
        if(id <= 0 || quantity <= 0 || price <= 0) {
            lv_obj_t * mbox = lv_msgbox_create(NULL, "error", "Please enter valid values", NULL, true);
            lv_obj_center(mbox);
            return;
        }

        // 保存到货品列表
        Goods new_goods = {
                .id = id,
                .quantity = quantity,
                .price = price
            };
        strncpy(new_goods.name, name_str, sizeof(new_goods.name)-1);
        new_goods.name[sizeof(new_goods.name)-1] = '\0'; // 确保终止

        strncpy(new_goods.type, type_str, sizeof(new_goods.type)-1);
        new_goods.type[sizeof(new_goods.type)-1] = '\0'; // 确保终止;
        if(find_node(goods_list,new_goods.id) == NULL)
        {
            Goodslist *new = new_node(new_goods);
            insert_node(goods_list,new);
            file_save_data(goods_list,"goodsdata.txt");
            char buf[256];
            snprintf(buf,sizeof(buf),"%s have been successfully added.",new_goods.name);
            log_add(LOG_ADD_ITEM,cur_user.username,cur_user.identity,buf);
        }else{
            lv_obj_t * mbox = lv_msgbox_create(NULL, "error", "The same kind of goods already exist.\n If you want to add quantity, please go to the inventory addition page.\n", NULL, true);
            lv_obj_center(mbox);
            return;
        }
        // 关闭输入窗口
        lv_obj_del(input_win);
        input_win = NULL;
    }
}

//关闭窗口
void close_win_event(lv_event_t *e) {
    lv_obj_del(input_win); // 删除窗口
    input_win = NULL;
}


//创建窗口
void create_input_window(lv_event_t *e) {
    if(input_win) return; // 防止重复创建

    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *label = lv_obj_get_child(target,0);
    char *txt = lv_label_get_text(label);

    // 创建模态背景窗口
    input_win = lv_win_create(lv_scr_act(), 30);
    lv_obj_set_size(input_win, 800, 480);
    lv_obj_center(input_win);
    lv_win_add_title(input_win, txt);
    // 关闭标志
    lv_obj_t *close_btn = lv_win_add_btn(input_win, LV_SYMBOL_CLOSE, 30);
    lv_obj_add_event_cb(close_btn, close_win_event, LV_EVENT_CLICKED, NULL);
    //设置键盘
    keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, NULL);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);

    if(strcmp(txt,"Put goods") == 0)        //上架商品
    {
        put_goods_cb();
    }else if(strcmp(txt,"Remove goods") == 0)//下架商品
    {
        remove_goods_cb();
    }
    else if(strcmp(txt,"inventory") == 0) //库存管理
    {
        inventory_goods_cb();
    }
    else if(strcmp(txt,"price editor") == 0) //价格编辑
    {
        goods_price_editor_cb();
    }
    else if(strcmp(txt,"show goods") == 0) //显示商品
    {
        show_goods_cb();
    }
    else if(strcmp(txt,"Modify user") == 0) //修改用户信息
    {
        modify_user_info_ui();
    }
}


